package com.leyou.sms.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.http.MethodType;
import com.aliyuncs.http.ProtocolType;
import com.leyou.common.utils.JsonUtils;
import com.leyou.sms.config.SmsConfiguration;
import com.leyou.sms.config.SmsProperties;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import static com.leyou.sms.constans.SmsConstants.*;

import java.util.Map;

@Slf4j
@Component
public class SmsUtils {
    @Autowired
    private SmsConfiguration configuration;
    @Autowired
    private IAcsClient acsClient;
    @Autowired
    private SmsProperties prop;

//该方法只发送短信验证码功能
    public void sendVerifyCode(String phone, String code) {
//          参数
        String param = String.format(VERIFY_CODE_PARAM_TEMPLATE, code);
//        发送短信
        sendMessage(phone, prop.getSignName(), prop.getVerifyCodeTemplate(), param);
    }


private void sendMessage(String phone,String signName,String template,String param){

    CommonRequest request = new CommonRequest();

    request.setProtocol(ProtocolType.HTTPS);
    request.setMethod(MethodType.POST);
    request.setDomain(prop.getDomain());
    request.setVersion(prop.getVersion());
    request.setAction(prop.getAction());
    request.putQueryParameter(SMS_PARAM_KEY_PHONE, phone);
    request.putQueryParameter(SMS_PARAM_KEY_SIGN_NAME, signName);
    request.putQueryParameter(SMS_PARAM_KEY_TEMPLATE_CODE, template);
    request.putQueryParameter(SMS_PARAM_KEY_TEMPLATE_PARAM, param);
    try {
//        发送短信请求
        CommonResponse response = acsClient.getCommonResponse(request);
//        解析结果
        Map<String, String> resp = JsonUtils.toMap(response.getData(), String.class, String.class);
        // 判断是否是成功
        if (!StringUtils.equals(OK, resp.get(SMS_RESPONSE_KEY_CODE))) {
            // 不成功，
            log.error("【SMS服务】发送短信失败，原因{}", resp.get(SMS_RESPONSE_KEY_MESSAGE));
        }

        log.info("sms发送短信成功，手机号：{}", phone, response.getData());
    } catch (ServerException e) {
        log.info("sms发送短信失败,服务异常",e);
    } catch (ClientException e) {
        log.error("sms发送短信失败，客户端异常", e);
    }


}



}
